import { getMenu } from '@/api';

const permission = {
  state: {
    menuList: [], // 菜单表
    routeList: [], // 路由表
    permissionList: [], // 权限表
  },
  mutations: {
    SET_MENU: (state, menu) => {
      state.menuList = menu;
    },
    SET_ROUTE: (state, routeList) => {
      state.routeList = routeList;
    },
    SET_PERMISSION: (state, permission) => {
      state.permissionList = permission;
    },
  },
  actions: {
    GetMenu({ commit, state }) {
      return new Promise((resolve) => {
        getMenu().then((res) => {
          const menu = res.data;
          let arr = [];
          function addR(menu) {
            menu.forEach((el) => {
              if (el.componentName != null) {
                arr.push(el.componentName);
              }
              if (el.subMenus.length != 0) {
                addR(el.subMenus);
              }
            });
          }
          addR(menu);
          commit('SET_MENU', menu);
          commit('SET_PERMISSION', arr);
          resolve(arr);
        });
      });
    },
  },
};

export default permission;
